# -*— coding: utf-8 -*-
# @Time: 2024/5/27 10:49
# @Author :Lin Zhou
# @Email : lin1013143@163.com
# @File : bofeng.py
# @SoftWare: PyCharm
import json
import os

import requests

from Base import Log, GetYamlFile
from Base.HttpBase import HttpBase
from Base.ReadExcel import Read_Excel

log = Log.Log()
# base_url = 'http://192.168.17.236:30810'  # 独立租户2环境
# base_url = 'http://192.168.17.236:30510'  # 独立租户1环境
# tenant_id = 1  # 独立租户1环境
# base_url = 'http://111.20.218.54:30510'  # 凤县线上环境
base_url = 'http://10.8.0.109:30617'  # 铜川线上环境
# 需要手动修改所需环境id 及name
tenant_id = 2245
tenant_name = '海东市博锋矿业有限公司'

login_api = base_url + '/upms-provider/tenant/user/login/mobile'
login_data = {
    'mobile': '18368093710',
    'password': 'Aa123456'
}
headers = {
    'Content-Type': 'application/json'
}

is_list = []

token = HttpBase(login_api, login_data, 'post', headers).getHttpHeaders().get('access_token')
Refresh_token = HttpBase(login_api, login_data, 'post', headers).getHttpHeaders().get('Refresh_token')
log.info(token)
# 基础数据

# 基础请求头
base_headers = {
    'type': 'tenant',
    'tenant_id': str(tenant_id),
    'access_token': token,
    'Refresh_token': Refresh_token,
    # 'Version': 'middleground-2.0.5.6-SNAPSHOT'
}


def duizhang():
    """
    仅对照提供表格中月份中已有发货量客户的销售发货明细（含单价报表）
    """
    yml_list = GetYamlFile.getYamlFile()
    exTestPath = yml_list["exTest"]
    PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, exTestPath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("Sheet1")
    page_union_url = base_url + '/tenant-lms-basic-provide/lms/basic/customer/pageUnion'
    customer_name_not_in_list = []
    customer_name_list = []
    for i in range(0, num):
        car_num = readBook.read_Excel("Sheet1", i, 3)
        customer_code = readBook.read_Excel("Sheet1", i, 0)
        customer_code = customer_code.replace(" ", "")
        customer_data = {
            'status': 0,
            'auditStatus': 1,
            'current': 1,
            'size': 10,
            'nameOrCode': customer_code,
            'type': 1
        }
        result = requests.get(page_union_url, customer_data, headers=base_headers)
        try:
            customer_name = json.loads(result.content).get("data").get('records')[0].get('name')
            customer_id = json.loads(result.content).get("data").get('records')[0].get('id')
            dp_order_detail_price_page = base_url + '/tenant-lms-meter-provide/lms/meter/report/salesReport/dpOrderDetailPricePage'
            dp_order_detail_price_data = {
                'current': 1,
                'size': 10,
                'businessType': 2,
                'startTime': '2024-04-25 08:00:00',
                'endTime': '2024-05-25 07:59:59',
                'customerId': customer_id
            }
            price_result = requests.get(dp_order_detail_price_page, dp_order_detail_price_data, headers=base_headers)
            if json.loads(price_result.content)["data"]["records"]:
                price_len = len(json.loads(price_result.content)["data"]["records"])
                predict_weigh = json.loads(price_result.content)["data"]["records"][price_len - 1]["predictWeigh"]
                if str(car_num) != str(predict_weigh):
                    customer_name_list.append({"客户名称": customer_name, "表格销售量": car_num, "系统销售量": predict_weigh})
            else:
                customer_name_list.append({"客户名称": customer_name, "表格销售量": car_num, "系统销售量": 0})
        except IndexError:
            print(customer_data)
            print(json.loads(result.content).get("data"))
            customer_name_not_in_list.append({"客户名称": customer_code, "表格销售量": car_num, "系统销售量": 0})
    print(f"不存在客户：{customer_name_not_in_list}")
    print(f"数量对不上客户：{customer_name_list}")


if __name__ == '__main__':
    duizhang()
